/*
 * Bird.fx
 *
 * Created on 06.11.2009, 22:46:33
 */

package javafx179labs.nez.lab4;

import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Path;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.ArcTo;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.transition.AnimationPath;
import javafx.animation.transition.PathTransition;
import java.lang.Math;
import javafx.scene.input.MouseEvent;

/**
 * @author civil
 */

public class Bird extends CustomNode {

    public var x: Float;
    public var y: Float;
    def a = 7;

    var eyes: Group = Group {
        content: [
                Circle {
                    centerX: bind x - a
                    centerY: bind y + 3
                    radius: 6
                    fill: Color.YELLOW
                    stroke: Color.BLACK
                }
                Circle {
                    centerX: bind x + a
                    centerY: bind y + 3
                    radius: 6
                    fill: Color.YELLOW
                    stroke: Color.BLACK
                }
                Circle {
                    centerX: bind x + a
                    centerY: bind y + 3
                    radius: 2
                    fill: Color.BLACK
                }
                Circle {
                    centerX: bind x - a
                    centerY: bind y + 3
                    radius: 2
                    fill: Color.BLACK
                }

        ]
    }


    var body: Circle = Circle {
        centerX: bind x
        centerY: bind y
        radius: 10
    }

    var cx: Float = 0;
    var cy: Float = -10;
    var rWing: Path = Path {
        fill: null
        stroke: Color.BLACK
        elements: [
                MoveTo {
                    x: bind x
                    y: bind y
                }
                ArcTo {
                    sweepFlag: true
                    radiusX: 30
                    radiusY: 20
                    x: bind x + 60 + cx
                    y: bind y + 20 + cy
                }
        ]
    }

    var lWing: Path = Path {
        fill: null
        stroke: Color.BLACK
        elements: [
                MoveTo {
                    x: bind x
                    y: bind y
                }
                ArcTo {
                    radiusX: 30
                    radiusY: 20
                    x: bind x - 60 - cx
                    y: bind y + 20 + cy
                }
        ]
    }

    var tm: Timeline = Timeline {
        autoReverse: true
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
                KeyFrame {
                    time: 1s
                    values: [cx => -20, cy => 20]
                }
        ]
    }



    public function animation() {
        tm.play();
    }


    override public function create(): Node {
        Group {
            content: [lWing, rWing, body, eyes]
        }


    }
}

public function birdGenerator(c: Integer, time: Duration) {
    var birds: Bird[] = [];
    var timl = Timeline {
        repeatCount: c
        keyFrames: [
                KeyFrame {
                    time: time
                    action: function () {
                        var bird: Bird = Bird {
                            x: Math.random() * 1080
                            y: Math.random() * 400
                            onMouseClicked: function (e: MouseEvent) {
                                delete bird from birds;
                            }

                        }

                        var path: Path = Path {
                            elements: [
                                    MoveTo {
                                        x: Math.random() * 1280
                                        y: Math.random() * 400
                                    }
                                    ArcTo {
                                        x: Math.random() * 1280
                                        y: Math.random() * 400
                                        radiusX: Math.random() * 300
                                        radiusY: Math.random() * 200
                                    }
                            ]
                        }


                        var tr: PathTransition = PathTransition {
                            duration: (Math.random() * 9) * 1s
                            autoReverse: true
                            repeatCount: PathTransition.INDEFINITE
                            node: bird
                            path: AnimationPath.createFromPath(path)
                        }

                        insert bird into birds;
                        bird.animation();
                        tr.playFromStart();
                    }

                }
        ]
    }
    timl.playFromStart();

    return Group {
        content: bind birds
    }
}